package com.gx.cn.hospitalmanage;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.gx.cn.hospitalmanage.Control.TwitterRestClient;
import com.gx.cn.hospitalmanage.MyFragmentOne.Novel;
import com.gx.cn.hospitalmanage.MyFragmentOne.OutPatientService;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;

import java.util.LinkedList;
import java.util.List;

import cz.msebera.android.httpclient.Header;

import static android.widget.Toast.LENGTH_LONG;

/**
 * Created by JCQ on 2017/10/6.
 */

public class AActivity extends AppCompatActivity {

   // ListView listView;
    List<ItemObj> datas=new LinkedList<>();
//    List<ItemMenZhen> dataOutPas=new LinkedList<>();
    AsyncHttpClient m_HttpClient;
    MyAdapter m_MylsAdapter;
//    MyOutPatAdapter m_MyOutPatAdapter;
    ListView m_lsOffice;
    ListView m_lsMenZhen;


    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_a);
        m_lsOffice=(ListView) findViewById(R.id.listview);
        m_lsMenZhen= (ListView)findViewById(R.id.listview);

        m_MylsAdapter = new MyAdapter();


        m_lsOffice.setAdapter(m_MylsAdapter);

        //在list列表里选择某一行的id，查询id的科室对应的门诊
        m_lsOffice.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                TextView textView =(TextView) view.findViewById(R.id.ls_officeID);
                String officeId=textView.getText().toString();
                selectMenZhen(officeId);

                Toast.makeText(getApplicationContext(), textView.getText() , LENGTH_LONG).show();

            }
        });


        initData();


    }

    //选择看看哪个科室的有什么门诊
    public void selectMenZhen(String officeId){

        RequestParams params=new RequestParams();
        params.put("officeid",officeId);

        TwitterRestClient.get("logins/SelectOutPatientById.action", params, new AsyncHttpResponseHandler() {
            @Override
            public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {

                String json=new String(responseBody);

                Gson gson = new Gson();
                List<OutPatientService> applist = gson.fromJson(json,
                        new TypeToken<List<OutPatientService>>() {}.getType());
                for(OutPatientService app : applist) {

//                    dataOutPas.add(new ItemMenZhen(app.getOutpatientserviceid() , app.getOutpatientservicename()));
                }

//                m_MyOutPatAdapter.notifyDataSetChanged();

                Toast.makeText(getApplication(),"加载成功", LENGTH_LONG).show();

            }

            @Override
            public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {
                Toast.makeText(getApplication(),"加载失败", LENGTH_LONG).show();
            }
        });

    }

    //加载科室信息
    public void initData()
    {

        RequestParams params=new RequestParams();
      TwitterRestClient.get("logins/SelectAllOffice.action", params, new AsyncHttpResponseHandler() {
          @Override
          public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {

                 String json=new String(responseBody);

              Gson gson = new Gson();
              List<Novel> applist = gson.fromJson(json,
                      new TypeToken<List<Novel>>() {}.getType());
              for(Novel app : applist) {

                datas.add(new ItemObj(app.getOfficename(),app.getOfficeid()));
              }

              //通知列表更新
              m_MylsAdapter.notifyDataSetChanged();


              Toast.makeText(getApplication(),"加载成功", LENGTH_LONG).show();

          }

          @Override
          public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {
              Toast.makeText(getApplication(),"加载失败", LENGTH_LONG).show();
          }
      });


//        for(int i=0;i<50;i++)
//        {
//            datas.add(new ItemObj("this is No "+i));
//        }

    }

    class MyAdapter extends BaseAdapter {

        @Override
        public int getCount() {
            return datas.size();

        }

        @Override
        public Object getItem(int position) {
            return datas.get(position);
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {

            Holder holder;
            if(convertView==null)
            {
              convertView=getLayoutInflater().inflate(R.layout.listview_item,null);
                holder =new Holder();
                holder.tx=(TextView) convertView.findViewById(R.id.ls_tx);
                holder.txId=(TextView)convertView.findViewById(R.id.ls_officeID);
                convertView.setTag(holder);

            }else
            {
                holder=(Holder) convertView.getTag();

            }

            ItemObj item=(ItemObj) getItem(position);

            holder.tx.setText(item.mTxs);
            holder.txId.setText(item.officeId);

            return convertView;
        }

        class Holder{

            TextView tx;
            TextView txId;
        }
    }


    class ItemObj{

        public ItemObj(String tx ,String txId){
            mTxs=tx;
            officeId=txId;

        }


        public String mTxs;
        public String officeId;
    }






}
